﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UI
{
    class MyConfig
    {
        /// <summary>
        /// 得到APPConfig设置
        /// </summary>
        /// <param name="key">keyname</param>
        /// <returns></returns>
        public static string getAppValue(string key)
        {
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] != null)
            {
                return config.AppSettings.Settings[key].Value;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 设置Appconfig值
        /// </summary>
        /// <param name="key">keyname</param>
        /// <param name="value">keyvalue</param>
        /// <returns>0=失败 1=成功</returns>
        public static int? setAppValue(string key, string value)
        {
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);

            int saveTag = 0;
            if (config.AppSettings.Settings[key] != null)
            {
                config.AppSettings.Settings[key].Value = value;
                saveTag = 0;
            }
            else
            {
                config.AppSettings.Settings.Add(key, value);
                saveTag = 1;
            }


            config.Save(System.Configuration.ConfigurationSaveMode.Modified);
            return saveTag;

        }
    }
}
